package com.snxun.browser_light.widget.webview;

import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;

import com.lodz.android.pandora.widget.webview.PgWebViewClient;

/**
 * Created by Yangjw on 2020/11/20.
 */
public class CustomPageWebViewClient extends PgWebViewClient {
    /**
     * 判断webview是否加载成功
     */
    private boolean isError = false;
    /**
     * webview
     */
    private CustomPageWebView mWebView;
    /**
     * 加载错误显示的视图
     */
    private ViewGroup mErrorLayout;

    public CustomPageWebViewClient(CustomPageWebView mWebView, ViewGroup errorLayout) {
        this.mWebView = mWebView;
        this.mErrorLayout = errorLayout;
    }


    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        if (mErrorLayout != null) {
            if (isError) {
                mWebView.setVisibility(View.GONE);
                mErrorLayout.setVisibility(View.VISIBLE);
            } else {
                mWebView.setVisibility(View.VISIBLE);
                mErrorLayout.setVisibility(View.GONE);
            }
        }

    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
        isError = true;
    }
}
